//
//  TKYDesView.m
//  StationHouseHelper
//
//  Created by apple on 2018/7/26.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYDesView.h"
@interface TKYDesView()<UITextViewDelegate>
//textView的placeholder
@property (nonatomic, strong)UILabel *placeholderLabel;
@property (nonatomic, strong)UILabel *titleLabel;

@property (nonatomic, strong) UIView *bottomLineView;

@end
@implementation TKYDesView

- (instancetype)initWithFrame:(CGRect)frame titleLabelText:(NSString *)titleLabelText{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.titleLabel];
        self.titleLabel.text = titleLabelText;
        [self addSubview:self.textView];
        self.textView.delegate = self;
        
        [self addSubview:self.placeholderLabel];
        [self addSubview:self.bottomLineView];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(kWidthScale*10);
            make.top.equalTo(self).with.offset(0);
            make.width.mas_equalTo(kWidthScale*100);
            make.height.mas_equalTo(kHeightScale *20);
        }];
        
        [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(kWidthScale*10);
            make.right.equalTo(self).with.offset(-kWidthScale*10);
            make.top.equalTo(self.titleLabel.mas_bottom).with.offset(0);
            make.bottom.equalTo(self);
        }];
        
        [self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.textView).with.offset(0);
            make.top.equalTo(self.textView.mas_top).offset(5);
            make.right.equalTo(self.self.textView).with.offset(0);
            // make.bottom.equalTo(self.contentView).offset(-10);
            make.height.mas_equalTo(20);
        }];
        
        [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(kWidthScale*10);
            make.right.equalTo(self).with.offset(-kWidthScale*10);
            make.bottom.equalTo(self).with.offset(-kHeightScale *1);
            make.height.mas_equalTo(kWidthScale*1);
        }];
    }
    return self;
}

- (UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = @"任务标题";
        _titleLabel.font = Font(12);
       // _titleLabel.backgroundColor = [UIColor redColor];
        
    }
    return _titleLabel;
}

-  (UITextView *)textView{
    if (_textView == nil) {
        _textView = [[UITextView alloc]init];
        _textView.font = Font(12);
        
    }
    return _textView;
}

- (UILabel *)placeholderLabel{
    if (_placeholderLabel == nil) {
        _placeholderLabel = [[UILabel alloc]init];
        _placeholderLabel.font = Font(12);
        _placeholderLabel.textColor = kRGBColor(211, 211, 211, 1);
        // _placeholderLabel.text = @"请输入问题描述";
    }
    return _placeholderLabel;
}

- (UIView *)bottomLineView{
    if (_bottomLineView == nil) {
        _bottomLineView = [[UIView alloc]init];
        _bottomLineView.backgroundColor = kMainColour;
    }
    return _bottomLineView;
}

- (void)setPlaceholderStr:(NSString *)placeholderStr{
    _placeholderStr = placeholderStr;
    self.placeholderLabel.text = placeholderStr;
}

-(void)textViewDidChange:(UITextView *)textView
{
    self.placeholderLabel.text = @"";
    self.placeholderLabel.hidden = YES;
    //实时显示字数
    //    self.stringlenghtLab.text = [NSString stringWithFormat:@"%ld/100",(long)textView.text.length];
    
    //字数限制
    if (textView.text.length >= 100) {
        textView.text = [textView.text substringToIndex:100];
    }
    
    //取消按钮点击权限，并显示文字
    if (textView.text.length == 0) {
        self.placeholderLabel.hidden = NO;
        
    }
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        
        [self.textView resignFirstResponder];
        
        return NO;
    }
    
    if (self.describeTextBlock) {
        self.describeTextBlock(text);
    }
    
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if (self.describeTextBlock) {
        self.describeTextBlock(textView.text);
    }
    return YES;
}


@end
